<?php

namespace App\Listeners;

use App\Events\EmailEvent;
use App\Model\System\MailConfig;


class EmailListener extends Listener
{

    /**
     * 发送邮件
     * @param EmailEvent $event
     * @throws
     */
    public function handle(EmailEvent $event)
    {
        try{
            $mail = new \PHPMailer();
            $mail->IsSMTP();
            $config = MailConfig::find(1);
            $mail->Host = $config->mail_host;
            $mail->Port = $config->mail_port;
            $mail->SMTPSecure = $config->mail_smtpsecure;
            $mail->SMTPAuth = true;
            $mail->CharSet = 'UTF-8';
            $mail->Username = $config->mail_uername;
            $mail->Password = $config->mail_password;
            $mail->From = $config->mail_from_address;
            $mail->FromName = $config->mail_from_name;
            $mail->AddAddress($event->getAddress());
            $mail->WordWrap = 50;
            $mail->IsHTML($event->isHtml());

            $mail->Subject = $event->getSubject();
            $mail->Body = $event->getBody();
            $mail->AltBody = $event->getAltBody();

            $mail->Send();
        }catch (\Exception $e){
            throw  $e;
        }
    }

}
